package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.Timer;
import com.google.gwt.view.client.ListDataProvider;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Order;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Requester;
import com.weakteam.orderonline.adminconsole.gwt.client.util.Responser;

/**
 * Created by Max on 14.05.2014 for OrderOnline.
 */
public class OrdersList extends ListDataProvider<Order> implements Responser {

    public OrdersList() {
//        Requester request = new Requester();
//        request.addResponser(this);
//        request.getOrdersAll();
        final Requester request = new Requester();
        request.addResponser(OrdersList.this);
        request.getOrdersAll();
        new Timer() {
            @Override
            public void run() {
                request.getOrdersAll();
            }
        }.scheduleRepeating(6000);
    }

    @Override
    public void handleGetResponse(JSONArray data) {
        this.getList().clear();
        int n = data.size();
        if (n > 0) {
            for(int i = 0; i < n; i++) {
                JSONObject obj = data.get(i).isObject();
                final Order order = new Order(obj);
                this.getList().add(order);
            }
        }
    }

    @Override
    public void handlePostResponse(int responseCode) {

    }
}
